#!/bin/bash

# Flutter版本管理脚本
# 用法: ./version_manager.sh [操作] [版本类型]
# 操作: show, bump, set
# 版本类型: major, minor, patch, build

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

OPERATION=${1:-show}
VERSION_TYPE=${2:-patch}

# 获取当前版本信息
get_current_version() {
    local version_line=$(grep "^version:" pubspec.yaml)
    local version_part=$(echo "$version_line" | cut -d' ' -f2)
    VERSION=$(echo "$version_part" | cut -d'+' -f1)
    BUILD_NUMBER=$(echo "$version_part" | cut -d'+' -f2)
    
    MAJOR=$(echo "$VERSION" | cut -d'.' -f1)
    MINOR=$(echo "$VERSION" | cut -d'.' -f2)
    PATCH=$(echo "$VERSION" | cut -d'.' -f3)
}

# 显示当前版本
show_version() {
    get_current_version
    echo -e "${BLUE}📱 应用版本信息${NC}"
    echo -e "${YELLOW}应用名称:${NC} $(grep "^name:" pubspec.yaml | cut -d' ' -f2)"
    echo -e "${YELLOW}完整版本:${NC} ${VERSION}+${BUILD_NUMBER}"
    echo -e "${YELLOW}版本号:${NC} ${VERSION}"
    echo -e "  ${YELLOW}• 主版本 (Major):${NC} $MAJOR"
    echo -e "  ${YELLOW}• 次版本 (Minor):${NC} $MINOR"  
    echo -e "  ${YELLOW}• 补丁版本 (Patch):${NC} $PATCH"
    echo -e "${YELLOW}构建号:${NC} ${BUILD_NUMBER}"
}

# 更新版本号
bump_version() {
    get_current_version
    
    local new_major=$MAJOR
    local new_minor=$MINOR
    local new_patch=$PATCH
    local new_build=$BUILD_NUMBER
    
    case $VERSION_TYPE in
        "major")
            new_major=$((MAJOR + 1))
            new_minor=0
            new_patch=0
            new_build=$((BUILD_NUMBER + 1))
            ;;
        "minor")
            new_minor=$((MINOR + 1))
            new_patch=0
            new_build=$((BUILD_NUMBER + 1))
            ;;
        "patch")
            new_patch=$((PATCH + 1))
            new_build=$((BUILD_NUMBER + 1))
            ;;
        "build")
            new_build=$((BUILD_NUMBER + 1))
            ;;
        *)
            echo -e "${RED}❌ 不支持的版本类型: $VERSION_TYPE${NC}"
            echo -e "${YELLOW}支持的类型: major, minor, patch, build${NC}"
            exit 1
            ;;
    esac
    
    local new_version="${new_major}.${new_minor}.${new_patch}"
    local old_version_full="${VERSION}+${BUILD_NUMBER}"
    local new_version_full="${new_version}+${new_build}"
    
    echo -e "${BLUE}🔄 版本更新${NC}"
    echo -e "${YELLOW}当前版本:${NC} $old_version_full"
    echo -e "${YELLOW}新版本:${NC} $new_version_full"
    echo -e "${YELLOW}更新类型:${NC} $VERSION_TYPE"
    echo ""
    
    # 备份原文件
    cp pubspec.yaml pubspec.yaml.backup
    
    # 更新版本号
    sed -i.tmp "s/^version: .*/version: $new_version_full/" pubspec.yaml
    rm pubspec.yaml.tmp
    
    echo -e "${GREEN}✅ 版本号已更新！${NC}"
    echo -e "${YELLOW}💡 提示: 如果更新有误，可以使用以下命令恢复:${NC}"
    echo -e "   mv pubspec.yaml.backup pubspec.yaml"
}

# 设置指定版本
set_version() {
    if [ -z "$3" ]; then
        echo -e "${RED}❌ 请提供新版本号${NC}"
        echo -e "${YELLOW}用法: ./version_manager.sh set [版本号] [构建号]${NC}"
        echo -e "${YELLOW}示例: ./version_manager.sh set 2.0.0 10${NC}"
        exit 1
    fi
    
    local new_version=$2
    local new_build=${3:-1}
    local new_version_full="${new_version}+${new_build}"
    
    get_current_version
    local old_version_full="${VERSION}+${BUILD_NUMBER}"
    
    echo -e "${BLUE}🔄 设置版本${NC}"
    echo -e "${YELLOW}当前版本:${NC} $old_version_full"
    echo -e "${YELLOW}新版本:${NC} $new_version_full"
    echo ""
    
    # 验证版本号格式
    if [[ ! $new_version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
        echo -e "${RED}❌ 版本号格式错误，应为: x.y.z${NC}"
        exit 1
    fi
    
    # 备份原文件
    cp pubspec.yaml pubspec.yaml.backup
    
    # 更新版本号
    sed -i.tmp "s/^version: .*/version: $new_version_full/" pubspec.yaml
    rm pubspec.yaml.tmp
    
    echo -e "${GREEN}✅ 版本号已设置！${NC}"
}

# 主逻辑
case $OPERATION in
    "show")
        show_version
        ;;
    "bump")
        bump_version
        ;;
    "set")
        set_version "$@"
        ;;
    *)
        echo -e "${RED}❌ 不支持的操作: $OPERATION${NC}"
        echo ""
        echo -e "${YELLOW}用法:${NC}"
        echo -e "  ${BLUE}查看版本:${NC} ./version_manager.sh show"
        echo -e "  ${BLUE}升级版本:${NC} ./version_manager.sh bump [major|minor|patch|build]"
        echo -e "  ${BLUE}设置版本:${NC} ./version_manager.sh set [版本号] [构建号]"
        echo ""
        echo -e "${YELLOW}示例:${NC}"
        echo -e "  ./version_manager.sh show"
        echo -e "  ./version_manager.sh bump patch"
        echo -e "  ./version_manager.sh bump minor"
        echo -e "  ./version_manager.sh set 2.0.0 1"
        exit 1
        ;;
esac